﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Model;
using DBServer;

namespace StudyLibrary.MenuForm
{
    public partial class frmUrl : Form
    {
        public bool _isSuccess = false;

        int _type = 0;//0:add   1:update

        public frmUrl()
        {
            InitializeComponent();
        }

        private void frmUrl_Load(object sender, EventArgs e)
        {

            BindDGV();

            //txt_Name.Text = _menu.Name;
            //txt_Path.Text = _menu.Path;
            //numericUpDown1.Value = _menu.ViewOrder;

            //txt_Name.Enabled = false;
        }

        private void BindDGV()
        {
            string sql = "SELECT * FROM Url ORDER BY ViewOrder DESC";
            DataSet ds = SQLiteHelper.ExecuteDataset(sql);
            dataGridView1.DataSource = ds.Tables[0];
            dataGridView1.Refresh();
        }

        private void btn_Sure_Click(object sender, EventArgs e)
        {
            string _name = txt_Name.Text.Trim();
            string _path = txt_Path.Text.Trim();
            int _order = Convert.ToInt32(numericUpDown1.Value);
            if (string.IsNullOrEmpty(_name) || string.IsNullOrEmpty(_path))
            {
                MessageBox.Show("information can't empty");
                return;
            }

            URL u = new URL();
            u.Name = _name;
            u.Path = _path;
            u.ViewOrder = _order;

            int re = NHibernateHelper.SaveObject(u);
            if (re > 0)
            {
                MessageBox.Show("successful");
                BindDGV();
                _isSuccess = true;
                txt_Name.Text = "";
                txt_Path.Text = "";
            }
            else
            {
                MessageBox.Show("Failure");
            }

            //string _sqlAdd = "INSERT INTO Url VALUES('" + _name + "','" + _path + "'," + _order + ")";
            //int re = SQLiteHelper.ExecuteNonQuery(_sqlAdd);
            //if (re > 0)
            //{
            //    MessageBox.Show("Successful");
            //    BindDGV();
            //    _isSuccess = true;
            //    txt_Name.Text = "";
            //    txt_Path.Text = "";
            //    //numericUpDown1.Value = _menu.ViewOrder;
            //}
            //else
            //{
            //    MessageBox.Show("Failure");
            //}

            

        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsBtn_Delete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows != null && dataGridView1.SelectedRows.Count > 0)
            {
                string _name = dataGridView1.SelectedRows[0].Cells["cName"].Value.ToString();

                //string sql = "DELETE FROM Url WHERE Name = '" + _name + "'";
                //int re = SQLiteHelper.ExecuteNonQuery(sql);

                URL u = new URL();
                u.Name = _name;

                int re = NHibernateHelper.DeleteObject(u);
                if (re > 0)
                {
                    MessageBox.Show("successful");
                    BindDGV();
                    _isSuccess = true;
                    txt_Name.Text = "";
                    txt_Path.Text = "";
                }
                else
                {
                    MessageBox.Show("Failure");
                }
            }
        }
    }
}
